package com.xbao.service.impl;

import com.github.pagehelper.PageHelper;
import com.xbao.constant.PathConst;
import com.xbao.core.util.MyBeanUtil;
import com.xbao.core.util.pub.tool.StringUtils;
import com.xbao.dao.*;
import com.xbao.model.*;
import com.xbao.model.req.ResultAddition;
import com.xbao.model.req.StuReq;
import com.xbao.model.vo.EmpVo;
import com.xbao.model.vo.OrgStuCountVo;
import com.xbao.model.vo.StuVo;
import com.xbao.model.vo.WaitDealStuTotalVo;
import com.xbao.service.OssFileService;
import com.xbao.service.RuleService;
import com.xbao.service.StuAccountService;
import com.xbao.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 18/8/24.
 */
@Service
public class StuServiceImpl implements StuService {

    private OssFileService ossFileService;

    @Resource
    private StuMapper stuMapper;
    @Resource
    private VStuMapper vStuMapper;


    @Resource
    private StuClsRelMapper stuClsRelMapper;

    @Autowired
    private StuExtraMapper extraMapper;

    @Autowired
    private AskAbsentMapper absentMapper;

    @Resource
    private RuleService ruleService;

    @Resource
    private StuAccountService stuAccountService;

    @Override
    public void addStu(StuReq stu) {
        stuMapper.insert(stu);
    }


    @Override
    public void updateStu(StuReq stu) {
        stuMapper.updateByPrimaryKeySelective(stu);
       /* StuExample example=new StuExample();
       StuExample.Criteria criteria= examplei.createCriteria();
       stuMapper.updateByExample()*/
    }


    @Override
    public void deleteStu(Long id) {
        stuMapper.deleteByPrimaryKey(id);
    }


    @Override
    public StuVo detail(Long sid) {
        Stu stu = stuMapper.selectByPrimaryKey(sid);
        StuVo vo = MyBeanUtil.copyProperties(stu, StuVo.class);
        return vo;
    }




    @Override
    public List<StuVo> queryStu(StuReq stuReq) {
        VStuExample stuExample = new VStuExample();
        VStuExample.Criteria criteria = stuExample.createCriteria();
        if (stuReq.getPager() != null) {
            PageHelper.startPage(stuReq.getPager().getPageNo(), stuReq.getPager().getPageSize());
            stuExample.setOrderByClause("id desc");
        }
        if (stuReq.getId() != null) {
            criteria.andIdEqualTo(stuReq.getId());
        }
        if (!StringUtils.isNull(stuReq.getName())) {
            criteria.andNameLike("%" + stuReq + "%");
        }
        if (stuReq.getType() != null) {
            criteria.andTypeEqualTo(stuReq.getType());
        }
        List<VStu> stus = vStuMapper.selectByExample(stuExample);
        List<StuVo> vos = MyBeanUtil.copyList(stus, StuVo.class);
        return vos;

    }


    public void resultAdd(List<EmpVo> vos, ResultAddition resultAddition) {
        for (EmpVo vo : vos) {
            if (resultAddition.getIrules()) {


            }

        }


    }


    public void wrapRemainRecords(List<StuVo> vos) {
        for (StuVo vo : vos) {
            //   vo.setRemainDays(remainDayOfStu(vo.getId(), vo.getOrgid()));
        }
    }

    public void wrapRemainDays(List<StuVo> vos) {
        for (StuVo vo : vos) {
            //  vo.setRemainRecords(remainPeriodOfStu(vo.getId(), vo.getOrgid()));
        }

    }


    @Override
    public List<StuVo> stuOfCls(Long cid) {
        StuClsRelExample relExample = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = relExample.createCriteria();
        criteria.andCidEqualTo(cid);
        List<StuClsRel> stus = stuClsRelMapper.selectByExample(relExample);
        List<StuVo> vos = new ArrayList<StuVo>();
        for (StuClsRel rel : stus) {
            StuVo vo = detail(rel.getSid());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<StuVo> queryStuView(StuReq stuReq) {
        VStuExample stuExample = new VStuExample();
        VStuExample.Criteria criteria = stuExample.createCriteria();
        if (stuReq.getPager() != null) {
            PageHelper.startPage(stuReq.getPager().getPageNo(), stuReq.getPager().getPageSize());
            stuExample.setOrderByClause("id desc");
        }
        if (stuReq.getId() != null) {
            criteria.andIdEqualTo(stuReq.getId());
        }
        if (!StringUtils.isNull(stuReq.getName())) {
            criteria.andNameLike("%" + stuReq + "%");
        }
        if (stuReq.getType() != null) {
            criteria.andTypeEqualTo(stuReq.getType());
        }
        List<VStu> stus = vStuMapper.selectByExample(stuExample);
        List<StuVo> vos = MyBeanUtil.copyList(stus, StuVo.class);
        return vos;
    }


    @Override
    public List<StuVo> futureBirthdayStus(StuReq stuReq) {
        StuClsRelExample relExample = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = relExample.createCriteria();
        criteria.andCidEqualTo(null);
        List<StuClsRel> stus = stuClsRelMapper.selectByExample(relExample);
        List<StuVo> vos = new ArrayList<StuVo>();
        for (StuClsRel rel : stus) {
            StuVo vo = detail(rel.getSid());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<StuVo> nocontractStus(StuReq stuReq) {
        StuClsRelExample relExample = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = relExample.createCriteria();
        criteria.andCidEqualTo(null);
        List<StuClsRel> stus = stuClsRelMapper.selectByExample(relExample);
        List<StuVo> vos = new ArrayList<StuVo>();
        for (StuClsRel rel : stus) {
            StuVo vo = detail(rel.getSid());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<StuVo> remainPeriodRecordLt(StuReq stuReq) {
        StuClsRelExample relExample = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = relExample.createCriteria();
        criteria.andCidEqualTo(null);
        List<StuClsRel> stus = stuClsRelMapper.selectByExample(relExample);
        List<StuVo> vos = new ArrayList<StuVo>();
        for (StuClsRel rel : stus) {
            StuVo vo = detail(rel.getSid());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<StuVo> endTimeLt(StuReq stuReq) {
        StuClsRelExample relExample = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = relExample.createCriteria();
        criteria.andCidEqualTo(null);
        List<StuClsRel> stus = stuClsRelMapper.selectByExample(relExample);
        List<StuVo> vos = new ArrayList<StuVo>();
        for (StuClsRel rel : stus) {
            StuVo vo = detail(rel.getSid());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<StuVo> noPeriodRecordStus(StuReq stuReq) {
        StuClsRelExample relExample = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = relExample.createCriteria();
        criteria.andCidEqualTo(null);
        List<StuClsRel> stus = stuClsRelMapper.selectByExample(relExample);
        List<StuVo> vos = new ArrayList<StuVo>();
        for (StuClsRel rel : stus) {
            StuVo vo = detail(rel.getSid());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<StuVo> waitAbsentDealStus(StuReq stuReq) {
        AskAbsentExample absentExample = new AskAbsentExample();
        AskAbsentExample.Criteria criteria = absentExample.createCriteria();
        criteria.andCidEqualTo(stuReq.getOrgid());
        List<AskAbsent> stus = absentMapper.selectByExample(absentExample);
        List<StuVo> vos = new ArrayList<StuVo>();
        for (AskAbsent absent : stus) {
            StuVo vo = detail(absent.getSid());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<StuVo> absendStus(StuReq stuReq) {

        StuClsRelExample relExample = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = relExample.createCriteria();
        criteria.andCidEqualTo(null);
        List<StuClsRel> stus = stuClsRelMapper.selectByExample(relExample);
        List<StuVo> vos = new ArrayList<StuVo>();
        for (StuClsRel rel : stus) {
            StuVo vo = detail(rel.getSid());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<StuVo> noBindWxStus(StuReq stuReq) {
        StuClsRelExample relExample = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = relExample.createCriteria();
        criteria.andCidEqualTo(null);
        List<StuClsRel> stus = stuClsRelMapper.selectByExample(relExample);
        List<StuVo> vos = new ArrayList<StuVo>();
        for (StuClsRel rel : stus) {
            StuVo vo = detail(rel.getSid());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<StuVo> absentDealStus(StuReq stuReq) {
        StuClsRelExample relExample = new StuClsRelExample();
        StuClsRelExample.Criteria criteria = relExample.createCriteria();
        criteria.andCidEqualTo(null);
        List<StuClsRel> stus = stuClsRelMapper.selectByExample(relExample);
        List<StuVo> vos = new ArrayList<StuVo>();
        for (StuClsRel rel : stus) {
            StuVo vo = detail(rel.getSid());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public Integer futureBirthdayStusCount(Long begintime, Long endtime, Long orgid) {
        StuExample stuExample = new StuExample();
        StuExample.Criteria criteria = stuExample.createCriteria();
        criteria.andBirthdayBetween(begintime, endtime);
        criteria.andOrgidEqualTo(orgid);
        Integer count = stuMapper.countByExample(stuExample);
        return count;

    }

    @Override
    public Integer nocontractStusCount(Long begintime, Long endtime, Long orgid) {
        StuExample stuExample = new StuExample();
        StuExample.Criteria criteria = stuExample.createCriteria();
        criteria.andFlagIsNotNull();
        criteria.andOrgidEqualTo(orgid);
        Integer count = stuMapper.countByExample(stuExample);
        return count;

    }

    @Override
    public Integer remainPeriodRecordLtCount(Integer releaseTime, Long orgid) {
        Integer count = extraMapper.remainRecordStusCount(releaseTime, orgid);
        return count;

    }

    @Override
    public Integer endTimeLtCount(Long begintime, Long endtime, Long orgid) {
        Integer count = extraMapper.remainDayLtStuCount(begintime, endtime, orgid);
        return count;
    }


    @Override
    public Integer waitAbsentDealStusCount(Long begintime, Long endtime, Long orgid) {
        StuExample stuExample = new StuExample();
        StuExample.Criteria criteria = stuExample.createCriteria();
        criteria.andBirthdayBetween(begintime, endtime);
        criteria.andOrgidEqualTo(orgid);
        Integer count = stuMapper.countByExample(stuExample);
        return count;
    }

    @Override
    public Integer absentDealStusCount(Long begintime, Long endtime, Long orgid) {
        StuExample stuExample = new StuExample();
        StuExample.Criteria criteria = stuExample.createCriteria();
        criteria.andBirthdayBetween(begintime, endtime);
        criteria.andOrgidEqualTo(orgid);
        Integer count = stuMapper.countByExample(stuExample);
        return count;
    }

    @Override
    public Integer noBindWxStusCount(Long begintime, Long endtime, Long orgid) {
        StuExample stuExample = new StuExample();
        StuExample.Criteria criteria = stuExample.createCriteria();
        criteria.andBirthdayBetween(begintime, endtime);
        criteria.andOrgidEqualTo(orgid);
        Integer count = stuMapper.countByExample(stuExample);
        return count;
    }

    @Override
    public Integer remainPeriodOfStu(Long sid, Long orgid) {


        return 0;
    }

    @Override
    public Integer remainDayOfStu(Long sid, Long orgid) {
        return 0;
    }

    @Override
    public OrgStuCountVo baseOrgStuCount(Long orgid) {

        //总数量
        Integer all = 0;
        Integer trycount = 0;
        Integer excount = 0;
        OrgStuCountVo vo = new OrgStuCountVo();
        vo.setTotalcount(all);
        vo.setTrycount(trycount);
        vo.setExpcount(excount);
        return vo;
    }


    @Override
    public WaitDealStuTotalVo waitDetailStusInfo(StuReq stuReq) {
        return null;
    }

    @Override
    public void uploadHead(InputStream inputStream) {
        String key = PathConst.OSS_KEY;
        String bucket = PathConst.OSS_HEAD_BUCKET;
        ossFileService.updloadFile(inputStream, key, bucket);

    }

    public void resultAdd(StuVo vo, ResultAddition addition) {
        if (addition.getIallrecord()) {
             StuAccount account=stuAccountService.aggAccount(vo.getId());
             vo.setTotalRecords(account.getTotalperiods());
             vo.setRemainRecords(account.getTotalperiods());

        }


    }


}
